package dev.polls.skycube;

import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.TabHost;
import android.widget.Toast;

public class TabHandler extends TabActivity  {
	
	private static final int PICK_IMAGE = 1;
	protected static Uri uri;
	public Intent in;
	final int result=1;
	
	Bitmap bitmap;
	UploadFtp ga = new UploadFtp();
	
	public void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tabhostmain);

	    Resources res = getResources(); // Resource object to get Drawables
	    TabHost tabHost = getTabHost();  // The activity TabHost
	    TabHost.TabSpec spec;  // Resusable TabSpec for each tab
	    Intent intent;  // Reusable Intent for each tab

	    // Create an Intent to launch an Activity for the tab (to be reused)
	    intent = new Intent().setClass(this, HomeActivity.class);

	    // Initialize a TabSpec for each tab and add it to the TabHost
	    spec = tabHost.newTabSpec("home").setIndicator("Home",
	                      res.getDrawable(R.drawable.ic_tab_home))
	                  .setContent(intent);
	    tabHost.addTab(spec);

	    intent = new Intent().setClass(this, GroupActivity.class);
	    spec = tabHost.newTabSpec("group").setIndicator("Group",
	                      res.getDrawable(R.drawable.ic_tab_group))
	                  .setContent(intent);
	    tabHost.addTab(spec);

	    intent = new Intent().setClass(this, FileActivity.class);
	    spec = tabHost.newTabSpec("file").setIndicator("Files",
	                      res.getDrawable(R.drawable.ic_tab_file))
	                  .setContent(intent);
	    tabHost.addTab(spec);

	    tabHost.setCurrentTab(0);
		
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
	    MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.menu, menu);
	    return true;
	}
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
	    switch (item.getItemId()) {
        case R.id.upload:     Toast.makeText(this, "choose a picture", Toast.LENGTH_SHORT).show();
        Intent intentBrowseFiles = new Intent(Intent.ACTION_GET_CONTENT);
	    intentBrowseFiles.setType("image/*");
	    startActivityForResult(intentBrowseFiles,PICK_IMAGE);
        break;
	        case R.id.icon:     android.os.Process.killProcess(android.os.Process.myPid()); break;
	        case R.id.logout:	SharedPreferences settings = getSharedPreferences("account_info",0);
	        					SharedPreferences.Editor editor = settings.edit();
	        					editor.clear();
	        					editor.commit();
	        					Intent i = new Intent(TabHandler.this, SkyCubeActivity.class);
	        					startActivity(i);
			Log.d("menu_pressed", "logout");
			finish();	break;
	    }
	    return true;
	}
	public void switchTab(int tab){
		TabHost tabHost = getTabHost();
        tabHost.setCurrentTab(tab);
}
	
	 protected void onActivityResult(int requestCode, int resultCode, Intent data)
	    {  
	    	System.out.println(resultCode+" "+requestCode);
	    	switch (requestCode)
	    	
	           {
	           case PICK_IMAGE:
	    	   if (resultCode == Activity.RESULT_OK) 
	    	   {
	    		   
	    		   Uri uri = data.getData();   
	    		   String path = getRealPathFromURI(uri);
	    		   Log.d("filepath",getRealPathFromURI(uri));
	    		   Intent b =new Intent(TabHandler.this,Image.class);
	    		   b.putExtra("pathId",path);
	    		   //startActivityForResult(b,result);
	    		   startActivity(b);
	    		   
	    		  // Intent a = new Intent(GroupActivity.this,Image.class);
	    		 //  startActivity(a);
	    		  
	    	   }
	           }
	    }
	    public String getRealPathFromURI(Uri contentUri) {
	        String[] proj = { MediaStore.Images.Media.DATA };
	        Cursor cursor = managedQuery(contentUri, proj, null, null, null);
	        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
	        cursor.moveToFirst();
	        return cursor.getString(column_index);
	        
	    }

}
